package cn.yuhao.party.school.Servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import cn.yuhao.party.pager.PageBean;
import cn.yuhao.party.school.dao.InschoolDao;
import cn.yuhao.party.school.domain.School;

public class InschoolServlet extends BaseServlet {
	private InschoolDao ns = new InschoolDao();
	public String editSchool(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		School School=CommonUtils.toBean(request.getParameterMap(), School.class);
		String date=getStringDate();
		School.setGtime(date);
		ns.editSchool(School);
		request.setAttribute("msg", "修改校外活动成功");
		return "f:/adminjsps/success.jsp";
	}
	public String AdminFindById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String gtheme = request.getParameter("Gtheme");
		String inorout=request.getParameter("inorout");
		School School = ns.findById(gtheme); 
		request.setAttribute("school", School);
		request.setAttribute("inorout", inorout);
		return "f:/jsps/login/welcome/Schoolcon.jsp";
	}
	public String reload(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String gtheme=request.getParameter("gtheme");
		School School=ns.findById(gtheme);
		
		request.setAttribute("school", School);
		return "f:/adminjsps/editschool.jsp";
	}
	public String delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String gtheme=request.getParameter("gtheme");
		ns.delete(gtheme);
		request.setAttribute("msg", "恭喜您校外活动删除成功");
		return "f:/adminjsps/msg.jsp";
	}
	
	public String addSchool(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		School School = CommonUtils.toBean(request.getParameterMap(), School.class);
	
		String date = getStringDate();
		School.setGtime(date);
		ns.addSchool(School);
		request.setAttribute("msg", "恭喜您校外活动添加成功");
		return "f:/adminjsps/msg.jsp";
		
	}

	public String adminFindAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int pc=getPc(request);
		String url=getUrl(request);
		PageBean<School> pb= (PageBean<School>) ns.findAll(pc);
		pb.setUrl(url);
		request.setAttribute("pb", pb);
		return "f:/adminjsps/school.jsp";
	}

	public String findAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int pc=getPc(request);
		String url=getUrl(request);
		PageBean<School> pb= (PageBean<School>) ns.findAll(pc);
		
		pb.setUrl(url);
		request.setAttribute("msg", "校内活动");
		request.setAttribute("msg1", "活动图片路径");
		request.setAttribute("pb", pb);
		return "f:/adminjsps/school.jsp";
	}

	public String findById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String gtheme = request.getParameter("gtheme");
		School School = ns.findById(gtheme);
		request.setAttribute("school", School);
		return "f:/jsps/pingtai/Schoolcon.jsp";
	}

	private static String getSub(String str) {
		if (str == null || str.equals(""))
			return " ";
		if (str.length() > 100)
			str = str.substring(0, 100) + "...";
		return str;
	}

	public static String getStringDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = formatter.format(currentTime);
		return date;
	}
	/**
	 * 获取当前页码
	 * @param req
	 * @return
	 */
	private int getPc(HttpServletRequest req) {
		int pc = 1;
		String param = req.getParameter("pc");
		if(param != null && !param.trim().isEmpty()) {
			try {
				pc = Integer.parseInt(param);
			} catch(RuntimeException e) {}
		}
		return pc;
	}
	
	/**
	 * 截取url，页面中的分页导航中需要使用它做为超链接的目标！
	 * @param req
	 * @return
	 */
	
	private String getUrl(HttpServletRequest req) {
		String url = req.getRequestURI() + "?" + req.getQueryString();
		
		int index = url.lastIndexOf("&pc=");
		if(index != -1) {
			url = url.substring(0, index);
		}
		return url;
	}
}
